package com.example;


import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.zhongzichang.springiap.NotificationDecoder;
import com.github.zhongzichang.springiap.notification.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.cert.CertificateException;
import java.util.*;

    @RestController
    @RequestMapping("/app-store-notify")
    public class NotificationController {

        private NotificationDecoder notificationDecoder = new NotificationDecoderImpl();

        @PostMapping("/v2")
        public void handle(@RequestBody ResponseBodyV2 responseBodyV2){
            try {
                ResponseBodyV2DecodedPayload decodedPayload = notificationDecoder.decodePayload(responseBodyV2.getSignedPayload());
                String signedRenewalInfo = decodedPayload.getData().getSignedRenewalInfo();
                String signedTransactionInfo = decodedPayload.getData().getSignedTransactionInfo();

                JWSRenewalInfoDecodedPayload jwsRenewalInfoDecodedPayload = notificationDecoder.decodeRenewalInfo(signedRenewalInfo);
                JWSTransactionDecodedPayload jwsTransactionDecodedPayload = notificationDecoder.decodeTransaction(signedTransactionInfo);

                //业务逻辑
                if (decodedPayload.getNotificationType().equals("")) {
                    //退款
                }
                if (decodedPayload.getNotificationType().equals("")) {
                    //续订
                }


            } catch (Exception e) {
                System.out.println(e.getMessage());
            }

        }
    }
